﻿using SD.Toolkits.Json;
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations.Schema;

namespace SD.OpcServer.Models.Entities
{
    /// <summary>
    /// 变量
    /// </summary>
    public class Variable
    {
        #region # 构造器

        #region 00.无参构造器
        /// <summary>
        /// 无参构造器
        /// </summary>
        protected Variable()
        {
            this.Id = Guid.NewGuid();
        }
        #endregion

        #region 01.创建变量构造器
        /// <summary>
        /// 创建变量构造器
        /// </summary>
        /// <param name="variableNo">变量编号</param>
        /// <param name="variableName">变量名称</param>
        /// <param name="dataType">数据类型</param>
        /// <param name="unit">单位</param>
        /// <param name="keepRecord">是否记录历史</param>
        /// <param name="equipmentNo">设备编号</param>
        /// <param name="description">描述</param>
        /// <param name="valueDescriptions">值描述字典</param>
        public Variable(string variableNo, string variableName, string dataType, string unit, bool keepRecord, string equipmentNo, string description, IDictionary<string, string> valueDescriptions)
            : this()
        {
            #region # 验证

            if (!Constants.AvailableTypes.Keys.Contains(dataType))
            {
                throw new ArgumentOutOfRangeException(nameof(dataType), "数据类型不存在！");
            }

            #endregion

            this.Number = variableNo;
            this.Name = variableName;
            this.DataType = dataType;
            this.Unit = unit;
            this.KeepRecord = keepRecord;
            this.EquipmentNo = equipmentNo;
            this.Description = description;
            this.ValueDescriptionsStr = valueDescriptions?.ToJson();
        }
        #endregion

        #endregion

        #region # 属性

        #region 标识 —— Guid Id
        /// <summary>
        /// 标识
        /// </summary>
        [DatabaseGenerated(DatabaseGeneratedOption.None)]
        public Guid Id { get; private set; }
        #endregion

        #region 编号 —— string Number
        /// <summary>
        /// 编号
        /// </summary>
        public string Number { get; private set; }
        #endregion

        #region 名称 —— string Name
        /// <summary>
        /// 名称
        /// </summary>
        public string Name { get; private set; }
        #endregion

        #region 数据类型 —— string DataType
        /// <summary>
        /// 数据类型
        /// </summary>
        public string DataType { get; private set; }
        #endregion

        #region 单位 —— string Unit
        /// <summary>
        /// 单位
        /// </summary>
        public string Unit { get; private set; }
        #endregion

        #region 是否记录历史 —— bool KeepRecord
        /// <summary>
        /// 是否记录历史
        /// </summary>
        public bool KeepRecord { get; private set; }
        #endregion

        #region 节点标识 —— string NodeId
        /// <summary>
        /// 节点标识
        /// </summary>
        public string NodeId { get; private set; }
        #endregion

        #region 设备编号 —— string EquipmentNo
        /// <summary>
        /// 设备编号
        /// </summary>
        public string EquipmentNo { get; private set; }
        #endregion

        #region 描述 —— string Description
        /// <summary>
        /// 描述
        /// </summary>
        public string Description { get; private set; }
        #endregion

        #region 只读属性 - 值描述字典 —— IDictionary<string, string> ValueDescriptions
        /// <summary>
        /// 只读属性 - 值描述字典
        /// </summary>
        /// <remarks>键：变量值，值：变量值描述</remarks>
        public IDictionary<string, string> ValueDescriptions
        {
            get
            {
                return string.IsNullOrWhiteSpace(this.ValueDescriptionsStr)
                    ? new Dictionary<string, string>()
                    : this.ValueDescriptionsStr.AsJsonTo<IDictionary<string, string>>();
            }
        }
        #endregion

        #region 内部属性 - 值描述字典序列化字符串 —— string ValueDescriptionsStr
        /// <summary>
        /// 内部属性 - 值描述字典序列化字符串
        /// </summary>
        public string ValueDescriptionsStr { get; private set; }
        #endregion

        #endregion

        #region # 方法

        #region 修改变量 —— void UpdateInfo(string variableName, string unit...
        /// <summary>
        /// 修改变量
        /// </summary>
        /// <param name="variableName">变量名称</param>
        /// <param name="unit">单位</param>
        /// <param name="keepRecord">是否记录历史</param>
        /// <param name="equipmentNo">设备编号</param>
        /// <param name="description">描述</param>
        /// <param name="valueDescriptions">值描述字典</param>
        public void UpdateInfo(string variableName, string unit, bool keepRecord, string equipmentNo, string description, IDictionary<string, string> valueDescriptions)
        {
            this.Name = variableName;
            this.Unit = unit;
            this.KeepRecord = keepRecord;
            this.EquipmentNo = equipmentNo;
            this.Description = description;
            this.ValueDescriptionsStr = valueDescriptions?.ToJson();
        }
        #endregion

        #region 设置节点标识 —— void SetNodeId(string nodeId)
        /// <summary>
        /// 设置节点标识
        /// </summary>
        public void SetNodeId(string nodeId)
        {
            this.NodeId = nodeId;
        }
        #endregion

        #endregion
    }
}
